package com.homework.action;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.homework.bean.CourseBean;
import com.homework.bean.HomeworkBean;
import com.homework.bean.UserBean;
import com.homework.service.HomeworkService;

public class HomeworkRequirementAction {

	private HomeworkService homeworkService;
	private int courseId;
	private String homeworkName;
	private String describe;
	private String batch;

	public HomeworkRequirementAction() {
		// TODO Auto-generated constructor stub
		homeworkService = new HomeworkService();
	}

	public int getCourseId() {
		return courseId;
	}

	public void setCourseId(int courseId) {
		this.courseId = courseId;
	}

	public String getHomeworkName() {
		return homeworkName;
	}

	public void setHomeworkName(String homeworkName) {
		this.homeworkName = homeworkName;
	}

	public String getDescribe() {
		return describe;
	}

	public void setDescribe(String describe) {
		this.describe = describe;
	}

	public String getBatch() {
		return batch;
	}

	public void setBatch(String batch) {
		this.batch = batch;
	}

	// 获取作业批次
	public String getHomeworkBatch() throws SQLException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();

		// userBean
		UserBean userBean = new UserBean();
		userBean.setUserId(String.valueOf(session.getAttribute("userId")));
		// courseBean
		CourseBean courseBean = new CourseBean();
		courseBean.setCourseId(String.valueOf(this.getCourseId()));

		int batch = (homeworkService.getBatch(userBean, courseBean)) + 1;
		request.setAttribute("batch", batch);
		request.setAttribute("courseId", this.getCourseId());
		return "Success";
	}

	// 布置作业
	public String homeworkRequirement() {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		// userBean
		UserBean userBean = new UserBean();
		userBean.setUserId(String.valueOf(session.getAttribute("userId")));
		// courseBean
		CourseBean courseBean = new CourseBean();
		courseBean.setCourseId(String.valueOf(this.getCourseId()));
		// homeworkBean
		HomeworkBean homeworkBean = new HomeworkBean();
		homeworkBean.setHomeworkName(getHomeworkName());
		homeworkBean.setBatch(getBatch());
		homeworkBean.setDescribe(getDescribe());
		
		homeworkService.homeworkRequirement(userBean, courseBean, homeworkBean);
		return "Success";
	}
}
